package com.seatademo.orders.service.impl;

import com.seatademo.orders.entity.OrderTbl;
import com.seatademo.orders.feign.ProductFeignClient;
import com.seatademo.orders.mapper.OrderTblMapper;
import com.seatademo.orders.service.IOrderTblService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuguohua
 * @since 2021-09-01
 */
@Service
public class OrderTblServiceImpl extends ServiceImpl<OrderTblMapper, OrderTbl> implements IOrderTblService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void placeOrder1(String userId, String commodityCode, int count) {
        BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
        OrderTbl order = new OrderTbl()
                .setUserId(userId)
                .setCommodityCode(commodityCode)
                .setCount(count)
                .setMoney(orderMoney);
        baseMapper.insert(order);
        //远程减少库存
        productFeignClient.deduct(commodityCode, count);
    }

    @Override
    public void placeOrder2(String userId, String commodityCode, int count) {
        BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));

        OrderTbl order = new OrderTbl()
                .setUserId(userId)
                .setCommodityCode(commodityCode)
                .setCount(count)
                .setMoney(orderMoney);
        baseMapper.insert(order);
    }
}
